package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.patient.InspectionReportListActivity
import com.compass.doctor.ui.patient.adapter.PatientExamineAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.R
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.constant.REFRESH_EXAMINE
import com.compass.framework.dialog.CommonMessageDialog
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.getStringFromResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 患者检查记录
 * Date:2024/2/1 14:42
 */
class PatientExamineFragment : BaseVBFragment<FragmentBaseListBinding>(),
    OnLoadMoreListener,
    OnRefreshListener {

    private val patientViewModel: PatientViewModel by viewModels()
    private lateinit var mAdapter: PatientExamineAdapter
    private var mPage = 1
    private lateinit var patientId: String //患者id
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)!!
        }
        initRecyclerView()
    }


    private var onBottomClick = object : OnCustomButtonClick {
        override fun onButtonCallBack(position: Int, msgType: String?) {
            val item = mAdapter.getItem(position)
            item?.let { order ->
                when (msgType) {
                    "cancel" -> {

                        CommonMessageDialog.Builder(requireActivity())
                            .setTitle(getStringFromResource(R.string.dialog_tips_title))
                            .setMessage("是立即取消当前检验检查?")
                            .setConfirm(getStringFromResource(R.string.default_confirm))

                            .setCancel(getString(com.compass.framework.R.string.default_cancel))
                            .setonCancelListener {
                                it?.dismiss()
                            }
                            .setonConfirmListener {
                                patientViewModel.cancelInspection(order.id)
                                it?.dismiss()
                            }.create().show()

                    }


                }
            }

        }
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(false)
            setOnRefreshListener(this@PatientExamineFragment)
            setOnLoadMoreListener(this@PatientExamineFragment)
        }
        mAdapter = PatientExamineAdapter()

        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
        mAdapter.setBottomCallBack(onBottomClick)
        mBinding?.apply {
            llBottomBtn.gone()
            tvSubmit.text = "查看患者报告单"
            tvSubmit.onClick {
                InspectionReportListActivity.start(requireContext(), patientId, 0)
            }
        }
    }


    override fun initData() {
        getPatientInspectionList()

        LiveDataBus.get().with(REFRESH_EXAMINE, String::class.java)
            .observe(this) {
                //创建成功刷新
                getPatientInspectionList()
            }

        patientViewModel.cancelInspectionLiveData.observe(this) {
            it?.let {
                getPatientInspectionList()
            }
        }
        patientViewModel.inspectionRecordLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
//                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
    }


    private fun getPatientInspectionList() {
        showLoading()
        patientViewModel.getPatientInspectionList(patientId)

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getPatientInspectionList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getPatientInspectionList()
    }
}